package top.ewind.controller.admin.system;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.AuthorAnnoInterceptor;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import top.ewind.controller.common.AdminRootCtrl;
import top.ewind.jfinal.ext.authorize.Authorize;
import top.ewind.util.SecurityUtil;

@Authorize(name = "1", value = "权限管理")
public class RoleCtrl extends AdminRootCtrl {

	public void index() {
		render("index.html");
	}

	public void profile() {
		String filter = " nid ="+getSessionAttr("nid")+SecurityUtil.sqlFilter(getPara("param", ""));
		renderJson(Db.paginate(getParaToInt("page", 1), getParaToInt("rp", 10),"SELECT t.*","from sys_role t where"+filter));
	}

	@Authorize(name = "1_1", value = "新增")
	public void add() {
		// 解析模块列表
		List<Map<String, Object>> modelist = new ArrayList<Map<String, Object>>();
		Map<String, String> mmap = AuthorAnnoInterceptor.getAuthModelNames();
		if (mmap != null) {
			for (String key : mmap.keySet()) {
				String[] levels = key.split("_");
				Map<String, Object> pmap = new HashMap<String, Object>();
				pmap.put("id", key);
				pmap.put("name", mmap.get(key));
				if (levels.length > 1) {
					String subpid = "";
					for (int i = 0; i < levels.length - 1; i++) {
						subpid += levels[i] + "_";
					}
					pmap.put("pId", subpid.substring(0, subpid.length() - 1));
				} else {
					pmap.put("pId", 0);
				}
				modelist.add(pmap);
			}
			Collections.sort(modelist, new Comparator<Map<String, Object>>() {
				public int compare(Map<String, Object> o1, Map<String, Object> o2) {
					return ((String) o1.get("id")).compareTo((String) o2.get("id"));
				}
			});
			setAttr("modeljson", JSON.toJSONString(modelist));
		}
		render("edit.html");
	}

	@Authorize(name = "1_2", value = "修改")
	public void edit() {
		Record role = Db.findById("sys_role",getPara(0));
		if (role != null) {
			setAttr("sysRole", role);
			List<Map<String, Object>> modelist = new ArrayList<Map<String, Object>>();
			Map<String, String> mmap = AuthorAnnoInterceptor.getAuthModelNames();
			if (mmap != null) {
				for (String key : mmap.keySet()) {
					String[] levels = key.split("_");
					Map<String, Object> pmap = new HashMap<String, Object>();
					pmap.put("id", key);
					pmap.put("name", mmap.get(key));
					if (levels.length > 1) {
						String subpid = "";
						for (int i = 0; i < levels.length - 1; i++) {
							subpid += levels[i] + "_";
						}
						pmap.put("pId", subpid.substring(0, subpid.length() - 1));
					} else {
						pmap.put("pId", 0);
					}
					if (StrKit.notBlank(role.getStr("models"))
							&& role.getStr("models").indexOf("," + key + ",") != -1) {
						pmap.put("checked", true);
					}
					modelist.add(pmap);
				}
				Collections.sort(modelist, new Comparator<Map<String, Object>>() {
					public int compare(Map<String, Object> o1, Map<String, Object> o2) {
						return ((String) o1.get("id")).compareTo((String) o2.get("id"));
					}
				});
				setAttr("modeljson", JSON.toJSONString(modelist));
			}
			render("edit.html");
		} else
			renderNull();
	}

	@Authorize(name = "1_3", value = "删除")
	public void delete() {
		boolean ret = false;
		ret = Db.deleteById("sys_role",getPara("id"));
		if (ret)
			renderJson("msg", "success");
		else
			renderJson("msg", "删除失败，请重试！");
	}

	public void save() {
		boolean ret = false;
		Record sysrole = Db.findById("sys_role", getPara("sysRole.id"));
		if (sysrole!=null) {
			sysrole.set("rname", getPara("sysRole.rname")).set("models", getPara("sysRole.models")).set("remark",getPara("sysRole.remark"));
			ret = Db.update("sys_role",sysrole);
		} else {
			
			Record sysroles = new Record().set("nid", getSessionAttr("nid")).set("rname", getPara("sysRole.rname")).set("models", getPara("sysRole.models")).set("remark",getPara("sysRole.remark"));
			ret = Db.save("sys_role",sysroles);
		}
		if (ret)
			renderJson("msg", "success");
		else
			renderJson("msg", "保存失败，请重试！");
	}
}